package com.fh.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.pms.entity.ProductCategory;
import com.fh.pms.service.IProductCategoryService;
import com.fh.result.ResultObject;
import com.fh.search.CategorySearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author F
 * @since 2021-05-21
 */
@RestController
@RequestMapping("productcategory")
public class ProductCategoryController {

    @Autowired
    private IProductCategoryService productCategoryService;


    @GetMapping
    public ResultObject queryProductCategoryList(Page<ProductCategory> page,Long parentId){
        QueryWrapper<ProductCategory>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.orderByAsc("sort");
        productCategoryService.page(page, queryWrapper);

       return  ResultObject.success(page);
    }


   /* 左右按钮*/
    @PostMapping("/show")
    public ResultObject updateShowStatus(Long id,Integer showStatus){
        productCategoryService.updateShowStatus(id,showStatus);
        return ResultObject.success();
    }
    /* 左右按钮*/
    @PostMapping("/navStatus")
    public ResultObject updateFactoryStatus(Long id,Integer navStatus){
        productCategoryService.updateFactoryStatus(id,navStatus);
        return ResultObject.success();
    }

    @PostMapping
    public ResultObject saveOrUpdateCategoryList(@RequestBody ProductCategory category){
        productCategoryService.saveOrUpdateCategoryList(category);
        return ResultObject.success();
    }
    @GetMapping("/{categoryId}")
    public ResultObject getCategoryById(@PathVariable("categoryId") Long categoryId){

        return ResultObject.success(productCategoryService.getById(categoryId));
    }
    @DeleteMapping("/{categoryId}")
    public ResultObject deleteCategory(@PathVariable("categoryId") Long categoryId){
        productCategoryService.removeById(categoryId);
        return ResultObject.success();
    }

    /*新增上级分类*/
    @GetMapping("/parent")
    public ResultObject queryParentList(){
        QueryWrapper<ProductCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<ProductCategory> list=productCategoryService.list(queryWrapper);
        return ResultObject.success(list);
    }




/*---*/
    @GetMapping("/pidAllList")
    public ResultObject queryAllList(){
        return productCategoryService.queryAllList();
    }

}
